﻿using System.Linq;
using System.Text;
using DevExpress.XtraEditors.DXErrorProvider;
using Dogshow.Model;
using Dogshow.Win.Models;

namespace Dogshow.Win.Helpers
{
    public static class ValidationExtensions
    {
         public static void GetErrors(this IValidatable entity, ErrorInfo erroInfo)
         {
             var er = entity.Validate();
             if (!er.IsValid)
             {
                 var sb = new StringBuilder();
                 var errors = er.Errors.Select(x => x.ErrorMessage);
                 foreach (var error in errors)
                 {
                     sb.AppendLine(error);
                 }
                 erroInfo.ErrorText = sb.ToString();
                 erroInfo.ErrorType = ErrorType.Critical;
             }
         }

        public static string GetErrors(this IValidatable entity)
        {
            var er = entity.Validate();
            if (!er.IsValid)
            {
                var sb = new StringBuilder();
                var errors = er.Errors.Select(x => x.ErrorMessage);
                foreach (var error in errors)
                {
                    sb.AppendLine(error);
                }
                return sb.ToString();
            }
            return null;
        }

        public static void GetErrorsForProperty(this IValidatable entity, string property, ErrorInfo errorInfo)
        {
            var er = entity.Validate();
            if (!er.IsValid)
            {
                var errors = er.Errors.Where(x => x.PropertyName == property).Select(x => x.ErrorMessage);
                var sb = new StringBuilder();
                foreach (var error in errors)
                {
                    sb.AppendLine(error);
                }
                errorInfo.ErrorText = sb.ToString();
                errorInfo.ErrorType = ErrorType.Critical;
            }
        }
    }
}